<?php
/**
 * This file is lil-wide model file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppModel', 'Model');
/**
 * LilAppModel class
 *
 * @uses          AppModel
 * @package       lil
 * @subpackage    lil
 */
class LilAppModel extends AppModel {
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @return void
 */
	public function __construct($id = false, $table = null, $ds = null) {
		if ($table_prefix = Configure::read('Lil.tablePrefix')) {
			$this->tablePrefix = $table_prefix;
		}
		
		// set current user
		App::uses('LilCurrentUser', 'Lil.Lib');
		$this->currentUser = LilCurrentUser::getInstance();
		
		// set current area
		App::uses('LilCurrentArea', 'Lil.Lib');
		$this->currentArea = LilCurrentArea::getInstance();
		
		// create LilDate func
		App::uses('LilDateEngine', 'Lil.Lib');
		$this->LilDate = LilDateEngine::getInstance();
		
		$this->callPluginHandlers('before_construct_model');
		parent::__construct($id, $table, $ds);
		$this->callPluginHandlers('after_construct_model');
	}
/**
 * beforeSave method
 *
 * @param mixed $options
 * @return boolean
 */
	public function beforeSave($options) {
		$data = $this->callPluginHandlers('before_save_model', array(
			'data'    => $this->data,
			'options' => $options
		));
		if (isset($data['data'])) $this->data = $data['data'];
		if (isset($this->data['return'])) return (boolean)$this->data['return'];
		return true;
	}
/**
 * afterSave method
 *
 * @param boolean $created true if a new object was created
 * @return void
 */
	public function afterSave($created = null) {
		$data = $this->callPluginHandlers('after_save_model',
			array('data' => $this->data, 'id' => $this->id, 'created' => $created)
		);
		if (isset($data['data'])) $this->data = $data['data'];
	}
/**
 * beforeFind method
 *
 * @param mixed $queryData
 * @return void
 */
	public function beforeFind($queryData = null) {
		$queryData = $this->callPluginHandlers('before_find_model', $queryData);
		return $queryData;
	}
/**
 * isAuthorized method
 *
 * @return void
 */
	public function isAuthorized($user, $controller, $action) {
		return $this->callPluginHandlers('is_authorized_model', array(
			'user'       => $user,
			'controller' => $controller,
			'action'     => $action
		));
	}
/**
 * callPluginHandlers method
 *
 * @return mixed
 */
	public function callPluginHandlers($handler, $data = null) {
		App::uses('LilPluginRegistry', 'Lil.Lib'); $registry = LilPluginRegistry::getInstance();
		return $registry->callPluginHandlers($this, $handler, $data);
	}
}